package activity

import (
	"fmt"
	"gitee.com/golang-module/carbon"
	"server/cfg"
	Enplayer "server/game/EnPlayer"
	"server/game/bag"
	"strconv"
	"strings"
	"time"
)

type ActivityLogic struct {
	dao *ActivityDao
}

func NewActivityLogic() *ActivityLogic {
	return &ActivityLogic{
		NewActivityDao(),
	}
}

func (a *ActivityLogic) InitActivity(userId string) {
	configList := cfg.ConfigData.TbBasicShop.GetDataList()
	data := Activity{
		UserId:                userId,
		ActivityRecharges:     make(map[int32]ActivityRecharge, 0),
		UserLiBaoGouMai:       0,
		UserActionLeiJiGouMai: make(map[int32]ActionLeiJiGouMai, 0),
		UserActionQianDao:     make(map[int32]ActionQianDao, 0),
		UserActionTask:        make(map[int32]ActionTask, 0),
		Times:                 0,
		UserLiBaoVAlue:        0,
		UserChongZhiEDu:       0,
		LastQianDaoTime:       time.Now().Unix(),
	}
	for _, config := range configList {
		data.ActivityRecharges[config.Id] = ActivityRecharge{
			ShopId:     config.Id,
			MonthTimes: 0,
			DayTimes:   0,
		}
	}
	a.dao.Insert(data)
}
func (a *ActivityLogic) GetActivity(userId string) (Activity, error) {
	info, err := a.dao.GetInfo(userId)
	if err != nil {
		return info, err
	}
	return info, err
}

func (a *ActivityLogic) Buy(userId string, shopId int32) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	config := cfg.ConfigData.TbBasicShop.Get(shopId)
	info, err := a.dao.GetInfo(userId)
	if err != nil {
		return nil, err
	}
	num := strings.Split(config.Num, "_")
	dayNum, _ := strconv.Atoi(num[0])
	monthNum, _ := strconv.Atoi(num[1])
	if info.ActivityRecharges[shopId].DayTimes > int32(dayNum) || info.ActivityRecharges[shopId].MonthTimes > int32(monthNum) {
		return nil, fmt.Errorf("超过购买次数")
	}
	configItem := strings.Split(config.Item, "_")
	itemId, _ := strconv.Atoi(configItem[0])
	itemNum, _ := strconv.Atoi(configItem[1])
	bagList, err := bag.EnAddItem(userId, itemId, itemNum)
	activityRecharges := info.ActivityRecharges[shopId]
	activityRecharges.DayTimes = activityRecharges.DayTimes + 1
	info.ActivityRecharges[shopId] = activityRecharges
	err = a.dao.Update(userId, "activity_recharges", info.ActivityRecharges)
	if err != nil {
		return nil, err
	}
	ret["bag_list"] = bagList
	ret["activity"] = info
	return ret, nil
}

func (a *ActivityLogic) ChongZhi(userId string, shopId int32) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	config := cfg.ConfigData.TbRechargeShop.Get(shopId)
	info, err := a.dao.GetInfo(userId)
	if err != nil {
		return nil, err
	}
	type rewardInfo map[string]int
	rewardList := []rewardInfo{}
	checkShu := strings.Contains(config.Reward, "|")
	if checkShu {
		parts := strings.Split(config.Reward, "|")
		for _, part := range parts {
			itemConfig := strings.Split(part, "_")
			itemId, _ := strconv.Atoi(itemConfig[0])
			itemNum, _ := strconv.Atoi(itemConfig[1])
			bag.EnAddItem(userId, itemId, itemNum)
			rewardList = append(rewardList, rewardInfo{
				"itemId":  itemId,
				"itemNum": itemNum,
			})
		}
		ret["reward_list"] = rewardList
	} else {
		itemConfig := strings.Split(config.Reward, "_")
		itemId, _ := strconv.Atoi(itemConfig[0])
		itemNum, _ := strconv.Atoi(itemConfig[1])
		bag.EnAddItem(userId, itemId, itemNum)
		rewardList = append(rewardList, rewardInfo{
			"itemId":  itemId,
			"itemNum": itemNum,
		})
		ret["reward_list"] = rewardList
	}
	info.UserChongZhiEDu += config.Price
	userInfo, err := Enplayer.EnGetUserInfo(userId)
	userInfo.VipDianShu = userInfo.VipDianShu + int(config.VipExp)
	for key, userShouChong := range userInfo.ShouChongInfo {
		if userShouChong.DangWei == int(shopId) && userShouChong.IsShouChong == false {
			itemShouChongConfig := strings.Split(config.FirstreturnIngots, "_")
			itemShouChongId, _ := strconv.Atoi(itemShouChongConfig[0])
			itemShouChongNum, _ := strconv.Atoi(itemShouChongConfig[1])
			bag.EnAddItem(userId, itemShouChongId, itemShouChongNum)
			ret["shou_chong_item"] = map[string]int{
				"item_id":  itemShouChongId,
				"item_num": itemShouChongNum,
			}
			userInfo.ShouChongInfo[key].IsShouChong = true

		}
	}
	retVip, _ := a.vipReward(int32(userInfo.VipDianShu), userInfo.Vip)
	if retVip["add_level"] != 0 {
		userInfo.Vip = retVip["add_level"].(int)
	}
	for _, itemString := range retVip["add_vip_main_rewards_item"].([]string) {
		itemMainList := strings.Split(itemString, "_")
		itemMainRewardId, _ := strconv.Atoi(itemMainList[0])
		itemMainRewardNum, _ := strconv.Atoi(itemMainList[1])
		bag.EnAddItem(userId, itemMainRewardId, itemMainRewardNum)
	}
	for _, itemString := range retVip["add_vip_rewards_item"].([]string) {
		itemList := strings.Split(itemString, "_")
		itemRewardId, _ := strconv.Atoi(itemList[0])
		itemRewardNum, _ := strconv.Atoi(itemList[1])
		bag.EnAddItem(userId, itemRewardId, itemRewardNum)
	}
	Enplayer.EnChangeUserShouChong(userId, userInfo.ShouChongInfo)
	//userInfo.VipDianShu += int(config.VipExp)
	Enplayer.EnAddUserVipDianShu(userId, int32(userInfo.VipDianShu), userInfo.Vip)
	a.dao.Update(userId, "user_chong_zhi_e_du", info.UserChongZhiEDu)
	bagList, err := bag.EnGetUserItem(userId)
	if err != nil {
		return nil, err
	}
	ret["bag_list"] = bagList
	ret["activity"] = info
	ret["add_user_chong_zhi_e_du"] = config.Price
	ret["user_shou_chong_info"] = userInfo.ShouChongInfo
	ret["add_vip_main_rewards_item"] = retVip["add_vip_main_rewards_item"]
	ret["add_vip_rewards_item"] = retVip["add_vip_rewards_item"]
	return ret, nil
}
func (a *ActivityLogic) vipReward(vipDian int32, vipLevel int) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	ret["add_level"] = 0
	addVipMainRewardsItem := make([]string, 0, 2)
	addVipRewardsItem := make([]string, 0, 6)
	configList := cfg.ConfigData.TbVip.GetDataList()
	for _, config := range configList {
		if config.Experience <= vipDian && config.Level >= int32(vipLevel) {
			ret["add_level"] = int(config.Level + 1)
			addLevel := config.Level + 1 + 10000
			configNew := cfg.ConfigData.TbVip.Get(addLevel)
			if strings.Contains(configNew.MainRewards, "|") {
				rewardItemList := strings.Split(configNew.MainRewards, "|")
				for _, itemStr := range rewardItemList {
					addVipMainRewardsItem = append(addVipMainRewardsItem, itemStr)
				}
			} else if strings.Contains(configNew.Rewards, "_") {
				addVipMainRewardsItem = append(addVipMainRewardsItem, configNew.MainRewards)
			}
			if strings.Contains(configNew.Rewards, "|") {
				rewardItemList := strings.Split(configNew.Rewards, "|")
				for _, itemStr := range rewardItemList {
					addVipRewardsItem = append(addVipRewardsItem, itemStr)
				}
			} else if strings.Contains(configNew.Rewards, "_") {
				addVipRewardsItem = append(addVipRewardsItem, configNew.MainRewards)
			}
		}
	}
	ret["add_vip_main_rewards_item"] = addVipMainRewardsItem
	ret["add_vip_rewards_item"] = addVipRewardsItem
	return ret, nil
}
func (a *ActivityLogic) QianDao(userId string) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	info, err := a.dao.GetInfo(userId)
	if err != nil {
		return nil, err
	}
	if carbon.CreateFromTimestamp(info.LastQianDaoTime).DiffAbsInDays(carbon.Now()) > 1 {
		_, err = bag.EnAddItem(userId, RichQuickSecretSignInCheckItemId, RichQuickSecretSignInCheckItemNum)
		if err != nil {
			return nil, fmt.Errorf("补签物品没有")
		}
	}
	info.Times += 1
	info.LastQianDaoTime = carbon.Now().Timestamp()
	a.dao.QianDao(userId, info.Times)
	bagList, _ := bag.EnGetUserItem(userId)
	ret["bag_list"] = bagList
	ret["activity"] = info
	return ret, nil
}
func (a *ActivityLogic) Reward(userId string, rewardType int64, rewardId int32) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	info, err := a.dao.GetInfo(userId)
	if err != nil {
		return nil, err
	}
	var rewardItemId, rewardItemNum int32
	switch rewardType {
	case 1:
		rewardInfo, ok := info.UserActionQianDao[rewardId]
		if ok && rewardInfo.RewardInfoIdOne != 0 {
			return ret, fmt.Errorf("已经领取了")
		}
		if !ok {
			rewardInfo = ActionQianDao{RewardInfoIdOne: rewardId}

		} else {
			rewardInfo.RewardInfoIdOne = rewardId
		}
		info.UserActionQianDao[rewardId] = rewardInfo
		rewardItemId = RICH_QUICK_SECRET_SIGN_IN_FUNDID
		rewardItemNum = 1
		a.dao.Update(userId, "user_action_qian_dao", info.UserActionQianDao)
		break
	case 2:
		rewardInfo, ok := info.UserActionQianDao[rewardId]
		if ok && rewardInfo.RewardInfoIdTwo != 0 {
			return ret, fmt.Errorf("已经领取了")
		}
		if !ok {
			rewardInfo = ActionQianDao{RewardInfoIdTwo: rewardId}

		} else {
			rewardInfo.RewardInfoIdTwo = rewardId
		}
		info.UserActionQianDao[rewardId] = rewardInfo
		rewardItemId = RICH_QUICK_SECRET_RIICHFUNDID
		rewardItemNum = 1
		a.dao.Update(userId, "user_action_qian_dao", info.UserActionQianDao)
		break
	case 3:
		rewardInfo, ok := info.UserActionLeiJiGouMai[rewardId]
		if ok && rewardInfo.RewardInfoIdOne != 0 {
			return ret, fmt.Errorf("已经领取了")
		}
		if !ok {
			rewardInfo = ActionLeiJiGouMai{RewardInfoIdOne: rewardId}

		} else {
			rewardInfo.RewardInfoIdOne = rewardId
		}
		rewardItemId = RICH_QUICK_SECRET_RIICHFUNDID
		rewardItemNum = 1
		info.UserActionLeiJiGouMai[rewardId] = rewardInfo
		a.dao.Update(userId, "user_action_lei_ji_gou_mai", info.UserActionLeiJiGouMai)
		break
	case 4:
		rewardInfo, ok := info.UserActionLeiJiGouMai[rewardId]
		if ok && rewardInfo.RewardInfoIdTwo != 0 {
			return ret, fmt.Errorf("已经领取了")
		}
		if !ok {
			rewardInfo = ActionLeiJiGouMai{RewardInfoIdTwo: rewardId}

		} else {
			rewardInfo.RewardInfoIdTwo = rewardId
		}
		rewardItemId = RICH_QUICK_SECRET_RIICHFUNDID
		rewardItemNum = 1
		info.UserActionLeiJiGouMai[rewardId] = rewardInfo
		a.dao.Update(userId, "user_action_lei_ji_gou_mai", info.UserActionLeiJiGouMai)
		break
	case 5:
		if info.UserLiBaoGouMai > rewardId {
			return ret, fmt.Errorf("不要使用")
		}
		info.UserLiBaoGouMai = rewardId
		rewardItemId = RICH_QUICK_SECRET_RIICHFUNDID
		rewardItemNum = 1
		a.dao.Update(userId, "user_li_bao_gou_mai", info.UserLiBaoGouMai)
		break
	}

	bagList, _ := bag.EnAddItem(userId, int(rewardItemId), int(rewardItemNum))
	ret["bag_list"] = bagList
	ret["activity"] = info
	return ret, nil
}
